#
# Copyright Laureano Arcanio 2008
# Released under the MIT License
# laureano.arcanio@gmail.com
#


__all__ = ['metadata', 'Page', 'Tags', 'Attrs', ]

from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.orm import sessionmaker, mapper, relation

# Postgres
#engine = create_engine('postgres://laureano:adolphito@localhost/lymon ')

# SqlLite
engine = create_engine('sqlite:///lymon.db')

#Session = sessionmaker(bind=engine)

metadata = MetaData()

page_table = Table('page', metadata,
	Column('id', Integer, primary_key=True),
	Column('name', String, unique=True)
	)
	
tags_table = Table('tags', metadata,
	Column('id', Integer, primary_key=True),
	Column('tag', String),
	Column('pageId', Integer, ForeignKey('page.id')),
	)

attrs_table = Table('attrs', metadata,
	Column('id', Integer, primary_key=True),
	Column('name', String),
	Column('value', String),
	Column('tagId', Integer, ForeignKey('tags.id')))
	
# Creates the DataBase
#metadata.create_all(engine)

class ModelObject(object):
    def __init__(self,**kwargs):
        for key, value in kwargs.items():
        	if value: setattr(self, str(key), str(value))

class Page(ModelObject): pass
class Tags(ModelObject): pass
class Attrs(ModelObject): pass

mapper(Page, page_table, 
	properties={'tags':relation(Tags, backref='page', cascade="all, delete")}) 
	
mapper(Tags, tags_table,
	properties={'attrs':relation(Attrs, backref='tags', cascade="all, delete")})
mapper(Attrs, attrs_table) 



